﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TagNavigationViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="TagNavigationViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Tag.ViewModel
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics.Contracts;
    using System.Globalization;
    using System.Linq;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;

    using Hisaab.Business.Interfaces;
    using Hisaab.Client.Common;
    using Hisaab.Client.Common.BaseClasses;
    using Hisaab.Client.Common.Services;
    using Hisaab.Client.Model;
    using Hisaab.Client.Modules.Core.Transaction;
    using Hisaab.Common;

    using Microsoft.Practices.Prism;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="TagNavigationViewModel"/> type.
    /// </summary>
    public class TagNavigationViewModel : NavigationViewModel
    {
        /// <summary>
        /// Account object
        /// </summary>
        private readonly ITagManager _tagManager;

        /// <summary>
        /// The session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Selected Tag
        /// </summary>
        private Tag _selectedTag;

        /// <summary>
        /// Tags observable collection
        /// </summary>
        private ObservableCollection<Tag> _tags;

        /// <summary>
        /// Member variable _refreshCommand
        /// </summary>
        private ICommand _refreshCommand;

        /// <summary>
        /// Initializes a new instance of the <see cref="TagNavigationViewModel"/> class.
        /// </summary>
        public TagNavigationViewModel()
            : this(ServiceLocator.Current.GetInstance<ITagManager>(), ServiceLocator.Current.GetInstance<ISession>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TagNavigationViewModel"/> class.
        /// </summary>
        /// <param name="tagManager">The tag manager.</param>
        /// <param name="session">The session.</param>
        public TagNavigationViewModel(ITagManager tagManager, ISession session)
        {
            Contract.Requires<ArgumentNullException>(tagManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            _tagManager = tagManager;
            _session = session;

            Title = "Tags";
            Image = new BitmapImage(
                        new Uri(
                        "Pack://application:,,,/Hisaab.Client.Common;component/Resources/Images/32/Tag.png",
                        UriKind.Absolute));

            ImageSmall =
                        new BitmapImage(
                        new Uri(
                        "Pack://application:,,,/Hisaab.Client.Common;component/Resources/Images/16/Tag.png",
                        UriKind.Absolute));

            GlobalCommands.Refresh.RegisterCommand(RefreshCommand);
            RefreshList();
        }

        /// <summary>
        /// Gets the refresh command.
        /// </summary>
        /// <value>
        /// The refresh command.
        /// </value>
        public ICommand RefreshCommand
        {
            get
            {
                if (_refreshCommand == null)
                {
                    _refreshCommand = new DelegateCommand(RefreshList);
                }

                return _refreshCommand;
            }
        }

        /// <summary>
        /// Gets the accounts.
        /// </summary>
        public ObservableCollection<Tag> Tags
        {
            get
            {
                return _tags;
            }

            private set
            {
                if (value != _tags)
                {
                    _tags = value;

                    RaisePropertyChangedEvent("Tags");
                }
            }
        }

        /// <summary>
        /// Gets or sets the selected tag.
        /// </summary>
        /// <value>
        /// The selected tag.
        /// </value>
        public Tag SelectedTag
        {
            get
            {
                return _selectedTag;
            }

            set
            {
                if (_selectedTag != value)
                {
                    _selectedTag = value;
                    RaisePropertyChangedEvent("SelectedTag");
                    OnTagChanged();
                }
            }
        }

        /// <summary>
        /// Refreshes the list.
        /// </summary>
        public void RefreshList()
        {
            Tags = new ObservableCollection<Tag>(_tagManager.List(new Business.Entities.Tag { UserId = _session.LoggedUser })
                .OrderBy(x => x.Name)
                .Select(x => x.Map<Business.Entities.Tag, Tag>()));
        }

        /// <summary>
        /// Called when [tag changed].
        /// </summary>
        private void OnTagChanged()
        {
            if (SelectedTag != null)
            {
                // Initialize
                var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

                // Show Workspace
                var query = new UriQuery { { TransactionModule.NavigationParameters.TagId, SelectedTag.TagId.ToString(CultureInfo.InvariantCulture) } };
                var dashboardUri = new Uri(TransactionModule.TransactionsName + query, UriKind.RelativeOrAbsolute);
                regionManager.RequestNavigate(Regions.WorkspaceRegion, dashboardUri);

                LastNavigatedUri = dashboardUri;
            }
        }
    }
}